<?php

namespace Wechat\Controller;

use Think\Controller;
use Common\Service\CmccApiService;
use Common\Service\MerchantService;
use Common\Service\MerUserService;
use Common\Service\WxEventTreasureService;
use Common\Service\WxEventTreasureUserService;
use Common\Service\WxEventTreasureAwardService;

class TreasureController extends Controller {
  const SESSION_NAME = 'ccmc_cupid';
  const ERROR_URL = 'http://mp.weixin.qq.com/s?__biz=MjM5NTQzNzM1Mw==&mid=214867265&idx=3&sn=6504d316a112cedfb4143b37cc40153c#rd';

  var $share_icon = '';
  var $is_assist  = FALSE ;
  var $is_visitor = FALSE ;
  var $api         = null ;
  var $share_title = '伙结帮个忙！';
  var $share_desc = '不会挖元宝的不是好矿工！我离华为P8手机越来越近了，快来助我一臂之力吧！';
  
  function __construct() {
    parent::__construct();
    $this->api = CmccApiService::instance();
    $this->share_icon = U('Public/themes/wechat/treasure/images/share_icon.jpg' , '' , true , true );
  }

  public function index( $mer_id = 0 , $event_id = 0 , $user_id = 0 ) {
    $user_token = I('get.user_token' , '');
    $vuser_token = I('get.vuser_token' , '');
    
    if ( !empty($user_token)  ) {
      $this->is_visitor = FALSE ;
    } elseif ( !empty ($vuser_token) ) {
      $user_token = $vuser_token ;
      $this->is_visitor = TRUE  ;
    } else {
      //错误
      gameover('参数错误');
      //跳转
    }
    
    $this->_check_user_info($mer_id , $user_token , $this->is_visitor ); 
    $user_data = session( self::SESSION_NAME );
    
    //print_r( $user_data  );
    
    $data['mer_data'] = $this->_check_mer_info($mer_id);  //检查商户信息
    $event_data = $this->_check_event_info($mer_id , $event_id ); //检查活动信息

    //URL
    if ( $user_id == 0 ) {
      $url = "/$mer_id/$event_id/" . $user_data['user_id']  ; 
    } else {
      $url = "/$mer_id/$event_id/" . $user_id ; 
    }
    //取分享信息
    $get_wx_js_config_url = U('Wechat/Treasure/index' , '' , TRUE , TRUE ) . "/$mer_id/$event_id/0" ;
    $cur_url = U('Wechat/Treasure/result' , '' , TRUE , TRUE ) . $url ;

    $data['wx_js_config'] = $this->api->get_wx_js_config( $get_wx_js_config_url );
    $data['share_data'] = array(
      'icon' => $this->share_icon ,
      'title' => $this->share_title,
      'link' => $this->api->get_share_url($cur_url) ,
      'desc' => $this->share_desc ,
    );

    $data['event_data'] = $event_data ;
    $data['config'] = array(
      'mer_id' => $mer_id ,
      'event_id' => $event_id ,
      'is_visitor' => $this->is_visitor ,
      'index_url' => U('Wechat/Treasure/index') . $url,
      'result_url' => U('Wechat/Treasure/result') . $url ,
      'rule_url' => U('Wechat/Treasure/rule') . $url ,
    );
    $data['param'] = array(
    );
        
    $this->assign($data);
    $this->display();
  }

  /**
   * 挖宝及助力页
   * @param type $mer_id
   * @param type $event_id
   * @param type $user_id
   */
  function result( $mer_id = 0 , $event_id = 0 , $user_id = 0) {
    $data['mer_data']  = $this->_check_mer_info($mer_id);  //检查商户信息
    $data['event_data'] = $this->_check_event_info($mer_id , $event_id ); //检查活动信息
    
    if ( empty( $user_id ) ) {
      gameover('参数错误');
    }
        
    //取用户数据
    $MerUser = MerUserService::instance();
    $data['user_data'] = $MerUser->get_by_id( $user_id  );
      
    //取财宝信息
    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $data['my_data'] = $WxEventTreasureUser->get_by_uid($user_id , $event_id ) ;
    
    //取助力信息
    $WxEventTreasureUserAssist = \Common\Service\WxEventTreasureUserAssistService::instance();
    $data['assist_data'] = $WxEventTreasureUserAssist->get_by_cond(array(
      'event_id' => $event_id ,
      'assist_user_id' => $user_id ,
      'page_size'      => 5000 ,
    ));
    
    //print_r( $data['my_data'] );
    
    //取user_token 助力用户
    $user_token = I('get.vuser_token' , '');
    if( !empty( $user_token ) ) {
      $this->is_visitor = TRUE ;
      $this->_check_user_info($mer_id , $user_token , $this->is_visitor ); //取用户信息
    }

    $url = "/$mer_id/$event_id/" . $user_id ;
    $data['play_url'] = self::ERROR_URL;
    $data['config'] = array(
      'is_assist' => $this->is_visitor ,
      'event_id' => $event_id ,
      'do_assist_url' => U('Wechat/Treasure/do_assist') . '/' . $event_id . '/' . $user_id ,
      'rule_url' => U('Wechat/Treasure/rule') . $url  ,
    );
    
    //分享数据
    $cur_url = U('Wechat/Treasure/result' , '' , TRUE , TRUE ) . $url ; 

    $data['wx_js_config'] = $this->api->get_wx_js_config($cur_url);
    $data['share_data'] = array(
      'icon' => $this->share_icon ,
      'title' => $this->share_title ,
      'desc' => $this->share_desc ,
      'link' => $this->api->get_share_url($cur_url) ,
    );
    
    $this->assign($data);
    $this->display();
  }
  
  function do_assist( $event_id , $assist_user_id ) {
    $user_data = session( self::SESSION_NAME );
    
    if (empty( $user_data ) ) {
      $this->ajaxReturn(ajax_arr('用户信息不正确'));
    }
    
    //do assist
    $WxEventTreasure = WxEventTreasureService::instance();
    //$WxEventTreasure
    $ret = $WxEventTreasure->do_assist( $event_id , $user_data['user_id'] , $assist_user_id );
    $this->ajaxReturn($ret);
  }
  
  /**
   * 规则领奖页
   * @param type $mer_id
   * @param type $event_id
   * @param type $user_id
   */
  function rule( $mer_id = 0 , $event_id = 0  , $user_id = 0 ) {
    if ( empty( $user_id ) ) {
      gameover('参数错误');
    }
    
    $mer_data = $this->_check_mer_info($mer_id);  //检查商户信息
    $event_data = $this->_check_event_info($mer_id , $event_id ); //检查活动信息

    $data['mer_data'] = $mer_data;
    $data['event_data'] = $event_data ;
        
    //用户信息
    $user_data = session( self::SESSION_NAME );
    //print_r($user_data);
    $is_assist = FALSE ;

    if ( $user_data['user_id'] != $user_id ) {
      $is_assist = TRUE ;
    }
    
    $WxEventTreasureUser = WxEventTreasureUserService::instance();
    $event_user = $WxEventTreasureUser->get_by_uid($user_id, $event_id);
    
    //取奖品
    $WxEventTreasureAward = WxEventTreasureAwardService::instance();
    $data['award_data'] = $WxEventTreasureAward->get_by_cond(array(
      'event_id' => $event_id ,
      'sort' => 'rank',
      'order' => 'ASC',
    ));

    $data['config'] = array(
      'user_id' => $user_id ,
      'is_assist' => $is_assist ,
      'event_user' => $event_user ,
      'take_award_url' => U('Wechat/Treasure/take_award') . "/$event_id/$user_id",
    );
    
    //取分享信息
    //URL
    $url = "/$mer_id/$event_id/$user_id"  ;
    //取分享信息
    $cur_url = U('Wechat/Treasure/rule' , '' , TRUE , TRUE ) . $url ; 
    $share_url = U('Wechat/Treasure/result' , '' , TRUE , TRUE ) . $url ; 
    $data['wx_js_config'] = $this->api->get_wx_js_config($cur_url);
    $data['share_data'] = array(
      'icon' => $this->share_icon ,
      'title' => $this->share_title ,
      'desc' => $this->share_desc ,
      'link' => $this->api->get_share_url($share_url) ,
    );
    
    //print_r($data);
    $this->assign($data);
    $this->display(); 
  }
  
  function take_award( $event_id = 0  , $user_id = 0 ) {
    $award_id = I('post.award_id');
    
    $WxEventTreasure = WxEventTreasureService::instance();
    $ret = $WxEventTreasure->take_award($event_id  , $user_id , $award_id );
    
    $this->ajaxReturn($ret);
  }
  
  /**
   * 取用户信息
   * @param type $mer_id
   */
  function _check_user_info( $mer_id , $user_token , $visitor = FALSE ) {
    //根据user_token取用户信息
    $api_ret = $this->api->get_user_info_from_api( $user_token , $visitor );

    //print_r( $api_ret );
    if ( $api_ret['status'] != 1 ) {
      
      //gameover($api_ret['msg']);
      redirect( self::ERROR_URL );
      exit();
    }

    $MerUser = MerUserService::instance();
    if ( !$visitor ) {
      $user_local_data = $MerUser->get_by_mer_extid($mer_id, $api_ret['uid']);
    } else {
      $user_local_data = $MerUser->get_by_mer_extvid($mer_id, $api_ret['vid']);
    }
    
    if ( empty( $user_local_data ) ) {
      //新建用户
      $username = 'cmcc_' . rand_string(12)  ;
      $new_array = array(
        
        'mer_id'   => $mer_id ,
        'phone'    => isset($api_ret['phone']) ? $api_ret['phone'] : '' ,
        'icon'     => $api_ret['headimgurl'] ,
        'nickname' => empty( $api_ret['nickname']) ? $username : $api_ret['nickname'] ,
        'username' => $username ,
      );
      if ( !$visitor ) {
        $new_array['ext_id'] = $api_ret['uid'] ;
      } else {
        $new_array['ext_vid'] = $api_ret['vid'] ;
      }
      
      $user_local_data = $this->_create_cmcc_user($new_array );
    }
    
    $save_data = array(
      'user_id' => $user_local_data['id'],
      'user_token' => $user_token ,
    );
    
    session( self::SESSION_NAME , $save_data );
  }
    
  /**
   * 
   * @param type $new_user_data
   * @return type
   */
  function _create_cmcc_user( $new_user_data ) {
    $new_user_data['role_id'] = MerUserService::USER_ROLE ;
    $new_user_data['status'] = MerUserService::STATUS ;
    $new_user_data['password'] = md5('1234') ;
    
    $MerUser = MerUserService::instance();
    $data =  $MerUser->create($new_user_data);  
    if ( !$data['status'] ) {
      gameover('创建用户失败');
    }
    return $data;
  }
  
  /**
   * 检查商户信息
   * @param type $mer_id
   * @return type
   */
  function _check_mer_info( $mer_id ) {
    if( empty($mer_id) ) {
      gameover('商户参数错误');
    }

    //取商户信息
    $Merchant = MerchantService::instance();
    $mer_data = $Merchant->get_by_id($mer_id);

    if ( empty( $mer_data ) ) {
      gameover('商户信息错误');
    }
    return $mer_data ;
  }

  /**
   * 检查活动信息
   * @param type $mer_id
   * @param type $event_id
   * @return type
   */
  function _check_event_info( $mer_id , $event_id ) {
    $WxEventTreasureService = WxEventTreasureService::instance();
    $today = strtotime(date('Y-m-d'));
    $event_data = $WxEventTreasureService->get_by_id($event_id);
    if ( $mer_id != $event_data['mer_id'] ) {
      gameover('商户没有该活动');
    }
    
    if ( $today < strtotime($event_data['start_time']) ) {
      gameover('活动还未开始');
    }
    
    if ( $today > strtotime($event_data['end_time']) ) {
      gameover('活动已经结束');
    }
    return $event_data ;
  }
  

  

}
